package com.jymj.service.impl;

import com.jymj.dao.ApplyDao;
import com.jymj.dao.ApplyLandDao;
import com.jymj.dao.HujiZhaiDao;
import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.YlEntity;
import com.jymj.entity.dto.ApplyDto;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.zjd.*;
import com.jymj.service.ApplyFileService;
import com.jymj.service.ApplyLandService;
import com.jymj.service.YlService;
import com.jymj.service.ZhaiService;
import com.jymj.utils.FileUtile.FileUploadEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jymj.utils.ShiroUtils.getUserEntity;
import static com.jymj.utils.ShiroUtils.getUserId;

/**
 * Created by ${lc} on 2021/9/1.
 */
@Service
public class ApplyLandServiceLmpl implements ApplyLandService {
    @Autowired
    private ApplyLandDao applyLandDao;
    @Autowired
    private ApplyDao applyDao;
    @Autowired
    private ZhaiService zhaiService;
    @Autowired
    private ApplyFileService applyFileService;
    @Autowired
    private YlService ylService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLand(List<ApplyLandEntity> applyLandEntitys) {
        for (ApplyLandEntity applyLandEntity : applyLandEntitys) {
            applyLandEntity.setYlId(applyLandEntity.getYlEntity().getObjectid());
            applyLandEntity.setSqDate(new Date());
            applyLandEntity.setUserId(Integer.parseInt(getUserId().toString()));
            applyLandEntity.setCode(applyLandEntity.getYlEntity().getCode());
            applyLandEntity.setXzqmc(applyLandEntity.getYlEntity().getXzqmc());
            applyLandEntity.setZhen(applyLandEntity.getYlEntity().getTownname());
            applyLandEntity.setName(applyLandEntity.getZhaiEntity().getHousecount());
            applyLandEntity.setIphone(applyLandEntity.getZhaiEntity().getPhone());
            applyLandEntity.setApUserId(applyLandEntity.getZhaiEntity().getZhaiid());
            applyLandDao.saveEntity(applyLandEntity);
            saveOrUpdate(applyLandEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApplyLandFile saveFile(FileUploadEntity fileUploadEntity, Integer cltype) {
        ApplyLandFile applyLandFile = new ApplyLandFile();
        applyLandFile.setFileName(fileUploadEntity.getFileName())
                .setUrl(fileUploadEntity.getDataUrl())
                .setUpdate(new Date())
                .setLandId(-1)
                .setYlId(-1)
                .setCltype(cltype);
        applyLandDao.saveFile(applyLandFile);
        return applyLandFile;
    }

    @Override
    public ApplyLandFile queryObject(Integer id) {
        return applyLandDao.queryObject(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFile(Integer id) {
        applyLandDao.deleteFile(id);
    }

    @Override
    public List<ApplyLandEntity> queryList(ApplyDto applyDto) {
        return applyLandDao.queryList(applyDto);
    }

    @Override
    public  List<ApplyLandEntity> queryById(Integer id) {
        return applyLandDao.queryById(id);
    }

    @Override
    public List<ApplyLandEntity> queryByYlId(Integer objectid) {
        return applyLandDao.queryByYlId(objectid);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateLand(List<ApplyLandEntity> applyLandEntitys) {
        for (ApplyLandEntity applyLandEntity : applyLandEntitys) {
            applyLandDao.update(applyLandEntity);
            saveOrUpdate(applyLandEntity);
            //用地完结  翻建添加
            if (applyLandEntity.getSptype() == 3){
                ApplyEntity apply = new ApplyEntity();
                apply.setYlId(applyLandEntity.getYlId() == null ? null : applyLandEntity.getYlId());
                apply.setSqDate(new Date());
                apply.setUserId(Integer.parseInt(getUserId().toString()));
                apply.setCode(applyLandEntity.getCode() == null ? null : applyLandEntity.getCode());
                apply.setXzqmc(applyLandEntity.getXzqmc() == null ? null : applyLandEntity.getXzqmc());
                apply.setZhen(applyLandEntity.getZhen() == null ? null : applyLandEntity.getZhen());
                apply.setName(applyLandEntity.getName() == null ? null : applyLandEntity.getName());
                apply.setApUserId(applyLandEntity.getApUserId() == null ? null : applyLandEntity.getApUserId());
                apply.setIphone(applyLandEntity.getIphone() == null ? null : applyLandEntity.getIphone());
                apply.setSptype(4);
                apply.setJjphone(applyLandEntity.getJjphone() == null ? null : applyLandEntity.getJjphone());
                apply.setZjdArea(applyLandEntity.getZjdArea() == null ? null : applyLandEntity.getZjdArea());
                apply.setFjzdArea(applyLandEntity.getFjzdArea() == null ? null : applyLandEntity.getFjzdArea());
                apply.setJdgd(applyLandEntity.getJdgd() == null ? null : applyLandEntity.getJdgd());
                apply.setAddress(applyLandEntity.getAddress() == null ? null : applyLandEntity.getAddress());
                apply.setEast(applyLandEntity.getEast() == null ? null : applyLandEntity.getEast());
                apply.setSouth(applyLandEntity.getSouth() == null ? null : applyLandEntity.getSouth());
                apply.setWest(applyLandEntity.getWest() == null ? null : applyLandEntity.getWest());
                apply.setNorth(applyLandEntity.getNorth() == null ? null : applyLandEntity.getNorth());
                apply.setLandtype(applyLandEntity.getLandtype() == null ? null : applyLandEntity.getLandtype());
                apply.setJflx(applyLandEntity.getJflx() == null ? null : applyLandEntity.getJflx());
                apply.setXybztjbh(applyLandEntity.getXybztjbh() == null ? null : applyLandEntity.getXybztjbh());
                apply.setFtysjbz(applyLandEntity.getFtysjbz() == null ? null : applyLandEntity.getFtysjbz());
                apply.setHousehold(applyLandEntity.getHousehold() == null ? null : applyLandEntity.getHousehold());
                apply.setTitanic(applyLandEntity.getTitanic() == null ? null : applyLandEntity.getTitanic());
                apply.setHeadTitanic(applyLandEntity.getHeadTitanic() == null ? null : applyLandEntity.getHeadTitanic());
                applyDao.saveEntity(apply);
                //添加图片
                List<ApplyFileEntity> applyFileEntities = applyLandDao.queryFiles(applyLandEntity.getId());
                applyFileService.saveFiles(applyFileEntities,apply.getId());
                //修改院落状态
                YlEntity ylEntity = applyLandEntity.getYlEntity();
                ylEntity.setApptype(4);
                ylService.updateYl(ylEntity);
                //修改房屋
                applyLandDao.updateFw(apply.getId(),applyLandEntity.getId());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer[] ids) {
        //判断是否有分户 无则清楚yl的流程
        List<ApplyLandEntity> applyLandEntities = applyLandDao.getAll(ids,2);
        for (ApplyLandEntity applyLandEntity : applyLandEntities) {
            zhaiService.updateHu(applyLandEntity.getYlId(),applyLandEntity.getHousehold());
        }
        if (applyLandEntities == null || applyLandEntities.size() <= 0){
            for (ApplyLandEntity applyLandEntity : applyLandEntities) {
                ylService.updateAppTYpe(applyLandEntity.getYlId());
            }
        }
        applyLandDao.delete(ids);
    }

    @Override
    public KeyValueEntity queryStatistical(ApplyDto applyDto) {
        return applyLandDao.queryStatistical(applyDto);
    }

    @Override
    public List<KeyValueEntity> getSummary(ApplyDto applyDto) {
        applyDto.setSysUser(getUserEntity());
        List<KeyValueEntity> keyValueEntities = applyLandDao.getSummary(applyDto);
        KeyValueEntity keyValueEntity = new KeyValueEntity();
        keyValueEntity.setObject1("总计");
        keyValueEntity.setObject3(keyValueEntities.stream().mapToInt(z ->
                z.getObject3() == null ? 0 : Integer.valueOf(z.getObject3().toString())).sum());
        keyValueEntity.setObject4(keyValueEntities.stream().mapToInt(z ->
                z.getObject4() == null ? 0 : Integer.valueOf(z.getObject4().toString())).sum());
        keyValueEntity.setObject5(keyValueEntities.stream().mapToInt(z ->
                z.getObject5() == null ? 0 : Integer.valueOf(z.getObject5().toString())).sum());
        keyValueEntity.setObject6(keyValueEntities.stream().mapToInt(z ->
                z.getObject6() == null ? 0 : Integer.valueOf(z.getObject6().toString())).sum());
        keyValueEntity.setObject7(keyValueEntities.stream().mapToInt(z ->
                z.getObject7() == null ? 0 : Integer.valueOf(z.getObject7().toString())).sum());
        keyValueEntity.setObject8(keyValueEntities.stream().mapToInt(z ->
                z.getObject8() == null ? 0 : Integer.valueOf(z.getObject8().toString())).sum());
        keyValueEntity.setObject9(keyValueEntities.stream().mapToInt(z ->
                z.getObject9() == null ? 0 : Integer.valueOf(z.getObject9().toString())).sum());
        keyValueEntities.add(0,keyValueEntity);
        return keyValueEntities;
    }

    @Override
    public List<ApplyLandEntity> getAll(Integer[] ids,Integer type) {
        return applyLandDao.getAll(ids,type);
    }

    public void saveOrUpdate(ApplyLandEntity applyLandEntity){
        applyLandEntity.getYlEntity().setApptype(applyLandEntity.getSptype());
        ylService.updateYl(applyLandEntity.getYlEntity());
        //添加翻建房屋信息
        if (applyLandEntity.getApplyFwEntities() != null
                && applyLandEntity.getApplyFwEntities().size() > 0) {
            for (ApplyFwEntity applyFwEntity : applyLandEntity.getApplyFwEntities()) {
                List<ApplyFwEntity> applyFwEntities = new ArrayList<>();
                if (applyFwEntity.getId() != null && applyFwEntity.getId() != 0) {
                    applyDao.updateApplyFw(applyFwEntity);
                } else {
                    applyFwEntities.add(applyFwEntity);
                    applyDao.saveApplyFw(applyFwEntities, -1,
                            applyLandEntity.getYlId(),applyLandEntity.getId());
                }
            }
        }
        //修改翻建图片
        if (applyLandEntity.getApplyLandFiles() != null && applyLandEntity.getApplyLandFiles().size() > 0) {
            for (ApplyLandFile applyLandFile : applyLandEntity.getApplyLandFiles()) {
                applyLandFile.setLandId(applyLandEntity.getId());
                applyLandFile.setYlId(applyLandEntity.getYlId());
                applyLandDao.updateFile(applyLandFile);
            }
        }

        //修改户籍信息
        if (applyLandEntity.getZhaiEntities() != null && applyLandEntity.getZhaiEntities().size() > 0) {
            Integer flag = 0;
            for (ZhaiEntity zhaiEntity : applyLandEntity.getZhaiEntities()) {
                if (zhaiEntity.getIdCard() != null && applyLandEntity.getZhaiEntity().getIdCard() != null) {
                    if (zhaiEntity.getIdCard().equals(applyLandEntity.getZhaiEntity().getIdCard())) {
                        zhaiEntity.setFjhz(1);
                        flag += 1;
                    }
                }
            }
            if (flag <= 0) {
                applyLandEntity.getZhaiEntities().add(applyLandEntity.getZhaiEntity());
            }
            for (ZhaiEntity zhaiEntity : applyLandEntity.getZhaiEntities()) {
                //判断添加  修改
                zhaiEntity.setNoteTaker(String.valueOf(getUserId()));
                zhaiEntity.setYlId(applyLandEntity.getYlId());
                zhaiEntity.setCode(applyLandEntity.getYlEntity().getCode());
                zhaiEntity.setZhen(applyLandEntity.getYlEntity().getTownname());
                if (zhaiEntity.getZhaiid() != null && zhaiEntity.getZhaiid() != 0) {
                    zhaiService.update(zhaiEntity);
                } else {
                    zhaiService.save(zhaiEntity);
                }
            }
        } else {
            applyLandEntity.getZhaiEntity().setNoteTaker(String.valueOf(getUserId()));
            applyLandEntity.getZhaiEntity().setYlId(applyLandEntity.getYlId());
            applyLandEntity.getZhaiEntity().setCode(applyLandEntity.getYlEntity().getCode());
            applyLandEntity.getZhaiEntity().setZhen(applyLandEntity.getYlEntity().getTownname());
            applyLandEntity.getZhaiEntity().setHousehold(applyLandEntity.getHousehold());
            if (applyLandEntity.getZhaiEntity().getZhaiid() != null
                    && applyLandEntity.getZhaiEntity().getZhaiid() != 0) {
                zhaiService.update(applyLandEntity.getZhaiEntity());
            } else {
                zhaiService.save(applyLandEntity.getZhaiEntity());
            }
        }
    }


}
